<?php

class Auto_Loader 
{
	public static function INIT()
	{
	    set_include_path(get_include_path() . PATH_SEPARATOR . PATH_VENDORS);
	    set_include_path(get_include_path() . PATH_SEPARATOR . PATH_APP);
        
        require_once('Zend/Loader/Autoloader.php');
        $loader = Zend_Loader_Autoloader::getInstance();
        
	    spl_autoload_register(array('self','coreComponents'));
	    spl_autoload_register(array('self','componentEngine'));
	}

    public static function coreComponents($class)
	{
	    self::searchDirectory(PATH_COMPONENTS, $class);
	}
	
	public static function componentEngine($class)
	{
	    self::searchDirectoryFiles(PATH_COMPONENT_ENGINE, $class);
	}
    
    public static function searchDirectory($directory, $target)
    {
    
        if(self::searchDirectoryFiles($directory, $target)) {
            return;
        }
        
        $dir = $directory;
		
		while($dirs = glob($dir . DS . '*', GLOB_ONLYDIR)) 
		{
			foreach($dirs as $dirname) 
			{
				if(is_file($dirname . DS . $target . ".php"))
				{
				    set_include_path(get_include_path() . PATH_SEPARATOR . $dirname);
				    @require_once($dirname . DS . $target . ".php");
				    //echo $dirname . DS . $target . ".php" . "<br>";
				    break 2;
				}
			}
			
			$dir .= DS . '*';
		}
    }
    
    public static function searchDirectoryFiles($directory, $target)
    {
        if(is_file($directory . DS . $target . ".php"))
		{   
            @require_once($directory . DS . $target . ".php");
			return true;
        }
    }


}
?>